package cn.itcast.service.impl;

import cn.itcast.dao.OrderListDao;
import cn.itcast.dao.OrderSettingsDao;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderListService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private OrderSettingsDao orderSettingsDao;

    /**分页查询*/
    @Override
    public PageResult findAllOrderByPage(OrderQueryPageBean orderQueryPageBean) {
        PageHelper.startPage(orderQueryPageBean.getCurrentPage(),orderQueryPageBean.getPageSize());
        //分页查询
        List<Order> orderList = orderListDao.findAllOrderByPage(orderQueryPageBean);
        //封装
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }


    /**删除单个对象*/
    @Override
    public Boolean deleteOrderById(String id) {
        return orderListDao.deleteOrderById(id);
    }

    /**新增单个对象*/
    @Override
    public Boolean saveOrder(Order order) {
        //设置id
        order.setId(UUID.randomUUID().toString());

        Boolean result = orderListDao.saveOrder(order);
        if (!result){
            return false;
        }
        return true;
    }



    /**更新对象的信息*/
    @Override
    public Boolean updateOrder(Order order) {
        return orderListDao.updateOrder(order);
    }

    /**修改未到诊,到诊*/
    @Override
    public Boolean updateOrderArrivalById(String id,Integer arrival) {

        return  orderListDao.updateOrderArrivalById(id, arrival);
    }

    /**根据id 查询*/
    @Override
    public Order findOrderById(String id) {
        Order order = orderListDao.findOrderById(id);
        return order;
    }


    /**更新预约人数的数据*/
    @Override
    public void updateOrderReservations(Order order) {
        try {
            //类型转换
            String orderDate = DateUtils.parseDate2String(order.getOrderDate());
            //统计预约人数
            Integer conut = orderListDao.findOrderConutByOrderDate(orderDate);
            //更新已预约人数(写入t_orderSetting)
            orderSettingsDao.updateOrderSettingReservations(orderDate,conut);
        } catch (Exception e) {
            log.error("更新已预约人数失败",e);
        }
    }


}
